package cn.tedu.gateway.filter;



import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 判断请求参数里面是否携带token数据 并且 token数据为admin  正常转发请求
 * 如果不是 则不转发路由
 */

//@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {
   /*
   全局过滤器逻辑判断
    */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String name = exchange.getRequest().getQueryParams().getFirst("token");
        if (!StringUtils.equals(name,"admin")){
            System.out.println("签证失败");
            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            //完成响应
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    /**
     * 返回的是过滤器的优先级顺序，数值越小，优先级越高。
     * 这里设为0，表示优先级最高
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
